package com.gin.no_gay_alliance.adpter.pm;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;

import com.gin.common.utils.TimeAgo;
import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.enums.PrivateMessageStatus;
import com.gin.nga.response.field.pm.PrivateMessage;
import com.gin.no_gay_alliance.activity.PriMsgReadActivity;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.databinding.ItemPriMsgBinding;
import com.gin.no_gay_alliance.utils.ViewUtils;

import java.util.List;
import java.util.Locale;

/**
 * 私信适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/31 17:10
 **/
public class PriMsgAdapter extends BaseBindingRecyclerViewAdapter<ItemPriMsgBinding, PrivateMessage> implements View.OnClickListener {
    public static final String TAG = PriMsgAdapter.class.getSimpleName();

    public PriMsgAdapter(Context context) {
        super(context);
    }

    public PriMsgAdapter(@NonNull Context context, @NonNull List<PrivateMessage> data) {
        super(context, data);
    }

    @Override
    protected void onBindViewHolder(ViewHolder<ItemPriMsgBinding> holder, @NonNull ItemPriMsgBinding b, int position, PrivateMessage item) {
        List<PrivateMessageStatus> status = item.getStatus();

        b.tvTitle.setText(item.getTitle());
        ViewUtils.setGone(status == null || !status.contains(PrivateMessageStatus.MULTI), b.tvTagMulti);
        ViewUtils.setGone(status == null || !status.contains(PrivateMessageStatus.UNREAD), b.tvTagUnread);
        ViewUtils.setGone(status == null || !status.contains(PrivateMessageStatus.FROM_SYSTEM), b.tvTagFromSystem);
        b.tvFrom.setText(ObjectUtils.isEmpty(item.getFromUsername()) ? "系统消息" : item.getFromUsername());
        b.tvLastReply.setText(ObjectUtils.isEmpty(item.getLastReplyUsername()) ? "系统消息" : item.getLastReplyUsername());
        b.tvTimeCreated.setText(TimeAgo.ago(item.getTimeCreated()));
        b.tvTimeLastReply.setText(TimeAgo.ago(item.getLastModify()));
        b.tvReplyCount.setText(String.format(Locale.CHINA, "(%d)", item.getPostCount()));

        //绑定id
        b.getRoot().setTag(item);
        b.getRoot().setOnClickListener(this);
    }

    @NonNull
    @Override
    protected ItemPriMsgBinding buildBinding(LayoutInflater inflater) {
        return ItemPriMsgBinding.inflate(inflater);
    }

    @Override
    public void onClick(View v) {
        PrivateMessage msg = (PrivateMessage) v.getTag();
        // 打开阅读私信活动
        PriMsgReadActivity.startActivity(context, msg.getMessageId(), msg.getPostCount(), msg.getStatus().contains(PrivateMessageStatus.FROM_SYSTEM));
    }
}
